<template>
  <div class="goods-hot">
    <h3>{{ title }}</h3>
    <!-- 商品区块 -->
    <GoodItem v-for="good in goodList" :key="good.id" :good="good"/>
  </div>
</template>
<script>
import { computed, onMounted, ref } from 'vue'
import { findHotGoods } from '@/api/good'
import { useRoute } from 'vue-router'
import GoodItem from '@/views/Home/components/HomeGoods.vue'
export default {
  name: 'GoodsHot',
  components: {
    GoodItem
  },
  props: {
    type: {
      type: Number,
      default: 1 // 1代表24小时热销榜   2代表周热销榜    3代表总热销榜
    }
  },
  setup (props) {
    // 声明一个枚举对象
    const types = {
      1: '24小时热销榜',
      2: '周热销榜',
      3: '总热销榜'
    }
    // 自定义的地方
    // 1. 头部的标题(可通过props/插件)
    // 2. 要渲染的列表数据是不同的 由一个接口字段来决定的  type 1: '24小时热销榜'=>prop   2: '周热销榜'   3: '总热销榜'
    const title = computed(() => {
      return types[props.type]
    })

    // 发起请求  获取热榜数据
    const route = useRoute()
    const goodList = ref([])
    async function loadData () {
      const res = await findHotGoods({
        id: route.params.id,
        type: props.type
      })
      //   console.log(res)
      goodList.value = res.data.result
    }
    onMounted(() => {
      loadData()
    })

    return {
      types,
      title,
      goodList
    }
  }
}
</script>
<style scoped lang="less">
.goods-hot {
  h3 {
    height: 70px;
    background: @helpColor;
    color: #fff;
    font-size: 18px;
    line-height: 70px;
    padding-left: 25px;
    margin-bottom: 10px;
    font-weight: normal;
  }
}
</style>
